package com.tenement.action;

import com.tenement.domain.TenementHouseClient;
import com.tenement.domain.TenementUser;
import com.tenement.service.HouseOrClientService;
import com.tenement.utils.HoseOrClientVo;
import com.tenement.utils.Message;
import com.tenement.utils.Tools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : HouseOrClientAction  //类名
 * @Description : 通知action  //描述
 * @Author : 易嵘  //作者
 * @Date: 2020-06-10 21:18  //时间
 */
@Controller
@RequestMapping("houseOrClientAction")
public class HouseOrClientAction {

    @Resource
    HouseOrClientService houseOrClientService;

    @ResponseBody
    @RequestMapping("selectAll")
    public List<HoseOrClientVo> selectAll(HttpSession session){
        TenementUser user= (TenementUser) session.getAttribute("user");
        return houseOrClientService.selectAll(user);
    }

    @ResponseBody
    @RequestMapping("findAll")
    public Map<String,Object> findAll(TenementHouseClient houseClient, @RequestParam("page") Integer page, @RequestParam("limit") Integer limit){
        List<TenementHouseClient> list=houseOrClientService.findAll(houseClient,page,limit);
        Long overCount=houseOrClientService.overCount(houseClient);
        return Tools.maps(overCount,list);
    }

    @ResponseBody
    @RequestMapping("deleteById")
    public Message deleteById(String id, HttpServletRequest request){
        String ip = request.getRemoteAddr();
        HttpSession session=request.getSession();
        TenementUser user= (TenementUser) session.getAttribute("user");
        int row=houseOrClientService.deleteById(id,user,ip);

        return Tools.rows(row);
    }

    @ResponseBody
    @RequestMapping("selectLatest")
    public TenementHouseClient selectLatest(){
        return houseOrClientService.selectLatest();
    }

    @ResponseBody
    @RequestMapping("deleteAll")
    public Message deleteAll(@RequestParam(value = "ids[]",required = false) String ids[], HttpServletRequest request){
        String ip=request.getRemoteAddr();
        HttpSession session=request.getSession();
        TenementUser user= (TenementUser) session.getAttribute("user");
        int row=houseOrClientService.deleteAll(ids,user,ip);
        return Tools.rows(row);
    }

    @ResponseBody
    @RequestMapping("add")
    public Message add(TenementHouseClient houseClient, HttpSession session,HttpServletRequest request){
        String ip = request.getRemoteAddr();
        TenementUser user= (TenementUser) session.getAttribute("user");
        int row = houseOrClientService.add(houseClient,user,ip);

        return Tools.rows(row);
    }

    @ResponseBody
    @RequestMapping("findById")
    public TenementHouseClient findById(String houseClientId){
        return houseOrClientService.findById(houseClientId);
    }

    @ResponseBody
    @RequestMapping("showOne")
    public int showOne(String houseClientId,HttpSession session){
        TenementUser user= (TenementUser) session.getAttribute("user");
        return houseOrClientService.showOne(houseClientId,user.getUserId());
    }

    /**
     * 日期格式化
     *
     * @param binder
     */
    /*@InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值，false:不能为空值

    }*/
}
